using System;
using System.Collections.Generic;
using Gameplay.PVE.Utils;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using Yoozoo.HRP.Runtime;

namespace Gameplay.PVE.Effect
{
#pragma warning disable 0414
    public class BlackScreenEffect
    {
        private Material m_material;
        private Color m_color;
        private Camera m_effectCamera;
        private HCamera m_effectHCamera;
        private GameObject m_quad;
        private bool m_show;
        public MonoBehaviour mono { get; set; }
        public Camera mainCamera { get; set; }
        public bool isSupport { get; set; }
        public bool isBattleGroundCamera { get; set; }
        public void Initialize()
        {
            m_material = PveResourceManager.Instance.GetBlackScreenMat();
            isSupport = m_material != null;
            mainCamera = PveCameraManager.Instance.rpgMainCamera;
            if (isSupport)
	        {
                Init();
	        }
        }
        public void Show()
        {
            if (!isSupport) return;
            m_show = true;
            m_color = Color.Lerp(Color.white, Color.black, 0.7f);
            HCameraManager.EnableCamera(m_effectHCamera);
            mainCamera.allowMSAA = false;
            m_quad.SetActive(true);
            ChangeFov(PveCameraManager.Instance.rpgMainCamera.fieldOfView);
        }
        public void Hide()
        {
            if (!isSupport) return;
            m_show = false;
            HCameraManager.DisableCamera(m_effectHCamera);
            m_material.SetFloat("_Alpha", 0);
            m_quad.SetActive(false);
        }
        public void Update()
        {
            //m_effectCamera.fieldOfView = mainCamera.fieldOfView;
            if (fadeDuration != 0)
            {
                var percent = (Time.unscaledTime - fadeStartTime) / fadeDuration;
                if (percent >= 1)
                {
                    percent = 1;
                    fadeDuration = 0;
                }

                float alpha = startAlpha + percent * (targetAlpha - startAlpha);
                m_material.SetFloat("_Alpha", alpha);
            }

            if (m_show)
            {
                ChangeFov(PveCameraManager.Instance.rpgMainCamera.fieldOfView);
            }
        }

        private float fadeStartTime;
        private float fadeDuration;
        private float startAlpha;
        private float targetAlpha;
        
        public void ChangeAlpha(float alpha,float fadeTime)
        {
            fadeDuration = fadeTime;
            fadeStartTime = Time.unscaledTime;
            startAlpha = m_material.GetFloat("_Alpha");
            targetAlpha = alpha;
        }

        public void ChangeFov(float fov)
        {
            m_effectCamera.fieldOfView = fov;
        }

        private void Init()
        {
            m_effectCamera = new GameObject("BlackScreen", typeof(Camera)).GetComponent<Camera>();
            var c = m_effectCamera.gameObject.AddComponent<UniversalAdditionalCameraData>();
            c.renderType = CameraRenderType.Overlay;
            m_effectHCamera = m_effectCamera.gameObject.AddComponent<HCamera>();

            HCameraManager.DisableCamera(m_effectHCamera);
            m_effectCamera.transform.SetParent( mainCamera.transform);
            m_effectCamera.farClipPlane = mainCamera.farClipPlane;
            m_effectCamera.fieldOfView = mainCamera.fieldOfView;
            m_effectCamera.transform.localPosition = Vector3.zero;
            m_effectCamera.transform.localEulerAngles = Vector3.zero;
            m_effectCamera.clearFlags = CameraClearFlags.Depth;
            PveUtils.blackScreenLayer = LayerMask.NameToLayer(PveString.blackScreenLayer);
            PveUtils.defaultLayer = LayerMask.NameToLayer(PveString.defaultLayer);
            m_effectCamera.cullingMask = 1 << PveUtils.blackScreenLayer;
            m_effectCamera.allowMSAA = false;
            m_effectCamera.allowHDR = false;
            m_effectCamera.useOcclusionCulling = true;
            m_effectCamera.depth = mainCamera.depth + 0.5f;
            m_quad = new GameObject("Quad");
            m_quad.transform.SetParent(mainCamera.transform);
            Mesh mesh = new Mesh();
            mesh.vertices = new Vector3[4]
            {
                new Vector3(-0.5f,-0.5f,0),
                new Vector3(0.5f,0.5f,0),
                new Vector3(0.5f,-0.5f,0),
                new Vector3(-0.5f,0.5f,0)
            };
            mesh.triangles = new int[6] { 0, 1, 2, 1, 0, 3 };
            m_quad.AddComponent<MeshFilter>().sharedMesh = mesh;
            m_material.renderQueue = 4200;
            var meshRenderer = m_quad.AddComponent<MeshRenderer>();
            meshRenderer.sharedMaterial=m_material;
            meshRenderer.sortingOrder = 1000;
            m_quad.transform.localPosition = new Vector3(0, 0, 0.2f);
            m_quad.transform.localRotation = Quaternion.identity;
            m_quad.transform.localScale = new Vector3(2000, 2000, 1);

            m_quad.SetActive(false);
        }
    }
}
